/*  
=========================================================
  Options:
  veh = [base, [<startOpts>], [<Pilot Options>], [<CoPilot Options>] , [<target list>], <ondemand>, <haveAgent>, <agentOptions>] execVM "transportation\initHeli.sqf"
  
  Parameters:

  TES_startMarkerPos: Marking the position where Heli will start

  base: object (helipad) where will be based (and first waypoint when ondemand=false or if flying = false)  
  
  startOpts: [<flying>]
  	
=========================================================
*/
#include "macros.sqf"
private ["_unit","_heliTypes","_heliPilots","_mando"];
waitUntil {!isNil "TES_init_started"};
if (!isServer) exitWith {};
/* ========================================================= */
// Define Preferences
_heliTypes =["BAF_Merlin_HC3_D"];
_heliPilots =["Pilot"];
_heliArmy =["Poet_PMC","Dixon_PMC","Tanny_PMC","Reynolds_PMC","Ry_PMC","Soldier_Sniper_KSVK_PMC","Soldier_GL_PMC"];
_deserted = 360; //Seconds Heli need to be deserted before respawn
/* ========================================================= */
/*           Dont change this point bellow                   */
/* ========================================================= */
// Some Macros
#define GetRandomElem(_myarray) _myarray select (random (count _myarray - 1))
/* ========================================================= */
// Define variables
_targetMarkers = _this select 0;
_killer = _this select 1;
_position = GVAR(startMarkerPos);

/* ========================================================= */
_mygroup = createGroup civilian;
_armygroup = createGroup civilian;
/* ========================================================= */		
// Start monitoring the vehicle
_timeout = time + _deserted;
_firsttime=true;
_run=true;
_unit=null;
_helipilot = null;
_deployed = false;
while {_run} do {	
	if (!(_firsttime)) then { sleep (1 + random 1);};
	//sleep 0.8;
	/* =================== Firt Time or Dead =================== */	
	if ( _firsttime ) then { // Firtime = init all stuff
		_firsttime=false;
		_dead = false;
		_unit = createVehicle [GetRandomElem(_heliTypes), _position, [], 0, "FLY"];

		[_unit] join _mygroup ;
		_unit setDir _dir;
		_unit setBehaviour "CARELESS";
		/* ========================== Pilot ======================== */	
		_helipilot = _mygroup createUnit [GetRandomElem(_heliPilots), getPos _unit, [], 5, "NONE"]; // Pilot
		[_helipilot] join _mygroup ;
		_helipilot moveindriver _unit;
		_helipilot  assignAsDriver _unit; 
		//[_helipilot] orderGetIn true;

		/* ========================== Army ======================== */	
		for "_x" from 1 to 8  do {
			_soldier = _armygroup createUnit [GetRandomElem(_heliArmy), getPos _unit, [], 5, "NONE"];
			[_soldier] join _armygroup ;
			_soldier moveincargo _unit;
			_soldier assignAsCargo _unit; 
			[_soldier] orderGetIn true;
		};
		//[_unit, [(_targetMarkers select 0) - 40,(_targetMarkers select 1) - 40,0], 20, false] call FUNC(heliRoute); 
		_unitinit = "[_unit, [_targetMarkers], 30, true] execVM 'transportation\mando_heliroute_arma.sqf'";
		_unit setVehicleInit format ["%1;", _unitinit];
		processInitCommands;
		// _unit setVariable ["mando_heliroute", "busy"];
	};
	/* ========================================================= */		
	// hint format ["this %1",_unit getVariable "mando_heliroute"];
    // if ((getDammage _unit > 0.8) and ({alive _x} count crew _unit == 0)) then {_dead = true}; // crew may not be onboard

	// Check if the vehicle is deserted.
	if (_deserted > 0 and false) then //Dont check for now... crew may not be onboard
	{
		if ((getPosASL _unit distance _position > 10) and ({alive _x} count crew _unit == 0) and (getDammage _unit < 0.8)) then 
		{
			if ({alive _x} count crew _unit > 0) then {_dead = false}; 
			if ({alive _x} count crew _unit == 0) then {_dead = true; _nodelay =true}; 
			if !(alive _unit) then {_dead = true; _nodelay = false}; 
		};
	};
	//if ({alive _x} count crew _unit == 0) then {_dead = true; _nodelay =true}; 
	//if !(alive _unit) then {_dead = true; _nodelay = false}; 
    if (_dead) then {	
		hint "Heli Down";
		//if (_nodelay) then {sleep 0.1; _nodelay = false;} else {sleep _delay;};
		//if (_dynamic) then {_position = getPosASL _unit; _dir = getDir _unit;};
		//if (_explode) then {_effect = "M_TOW_AT" createVehicle getPosASL _unit; _effect setPosASL getPosASL _unit;};
		//sleep 0.1;
		//{ deleteVehicle _x } forEach (crew _unit); deleteVehicle _unit;

	};
	if (!_deployed and (_unit getVariable "mando_heliroute" == "waiting")) then {
		//null = [_armygroup, _unit, _debug] execvm "transportation\combat_insertion.sqf"; 
		//nul = [_unit, 0,20] execVM "\norrn_dbo_fastrope\scripts\NORRN_fastRope_init.sqf";
		//hint "Deploy";
		//sleep 25;
		{
			[_x] joinSilent grpNull;
			unassignVehicle _x;
			[_x] orderGetIn false; 
			_x action ["eject",_unit];
			[_x] joinSilent _armygroup;
		} forEach units _armygroup;
		//hint "Hunt";
		[_armygroup,_killer] execVM "transportation\huntKiller.sqf";
		[_unit, [_position], 50, false] call FUNC(heliRoute);
		_deployed = true;
	};
	if (!_deployed and (_unit getVariable "mando_heliroute" == "damage")) then {
		hint "DEAD";
	};
};

hint "DONE";